{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Solution Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem: Given two 16 bit numbers, n and m, and two indices i, j, insert m into n such that m starts at bit j and ends at bit i.\n",
    "\n",
    "* [Constraints](#Constraints)\n",
    "* [Test Cases](#Test-Cases)\n",
    "* [Algorithm](#Algorithm)\n",
    "* [Code](#Code)\n",
    "* [Unit Test](#Unit-Test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Constraints\n",
    "\n",
    "* Can we assume j > i?\n",
    "    * Yes\n",
    "* Can we assume i through j have enough space for m?\n",
    "    * Yes\n",
    "* Can we assume the inputs are valid?\n",
    "    * No\n",
    "* Can we assume this fits memory?\n",
    "    * Yes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Cases\n",
    "\n",
    "* None as an input -> Exception\n",
    "* Negative index for i or j -> Exception\n",
    "* General case\n",
    "\n",
    "<pre>\n",
    "i = 2, j = 6\n",
    "                    j    i\n",
    "n      = 0000 0100 0011 1101\n",
    "m      = 0000 0000 0001 0011\n",
    "result = 0000 0100 0100 1101\n",
    "</pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Algorithm\n",
    "\n",
    "<pre>\n",
    "                    j    i\n",
    "n      = 0000 0100 0011 1101\n",
    "m      = 0000 0000 0001 0011\n",
    "\n",
    "lmask  = 1111 1111 1111 1111  -1\n",
    "lmask  = 1111 1111 1000 0000  -1 << (j + 1)\n",
    "\n",
    "rmask  = 0000 0000 0000 0001   1\n",
    "rmask  = 0000 0000 0000 0100   1 << i\n",
    "rmask  = 0000 0000 0000 0011   (1 << i) -1\n",
    "\n",
    "mask   = 1111 1111 1000 0011   lmask | rmask\n",
    "\n",
    "n      = 0000 0100 0011 1101\n",
    "mask   = 1111 1111 1000 0011   n & mask \n",
    "--------------------------------------------------\n",
    "n2     = 0000 0100 0000 0001\n",
    "\n",
    "n2     = 0000 0100 0000 0001\n",
    "mask2  = 0000 0000 0100 1100   m << i\n",
    "--------------------------------------------------\n",
    "result = 0000 0100 0100 1101   n2 | mask2\n",
    "</pre>\n",
    "\n",
    "Complexity:\n",
    "* Time: O(b), where b is the number of bits\n",
    "* Space: O(b), where b is the number of bits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Bits(object):\n",
    "\n",
    "    def insert_m_into_n(self, m, n, i, j):\n",
    "        if None in (m, n, i, j):\n",
    "            raise TypeError('Argument cannot be None')\n",
    "        if i < 0 or j < 0:\n",
    "            raise ValueError('Index cannot be negative')\n",
    "        left_mask = -1 << (j + 1)\n",
    "        right_mask = (1 << i) - 1\n",
    "        n_mask = left_mask | right_mask\n",
    "        # Clear bits from j to i, inclusive\n",
    "        n_cleared = n & n_mask\n",
    "        # Shift m into place before inserting it into n\n",
    "        m_mask = m << i\n",
    "        return n_cleared | m_mask"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting test_insert_m_into_n.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile test_insert_m_into_n.py\n",
    "import unittest\n",
    "\n",
    "\n",
    "class TestBit(unittest.TestCase):\n",
    "\n",
    "    def test_insert_m_into_n(self):\n",
    "        n = int('0000010000111101', base=2)\n",
    "        m = int('0000000000010011', base=2)\n",
    "        expected = int('0000010001001101', base=2)\n",
    "        bits = Bits()\n",
    "        self.assertEqual(bits.insert_m_into_n(m, n, i=2, j=6), expected)\n",
    "        print('Success: test_insert_m_into_n')\n",
    "\n",
    "\n",
    "def main():\n",
    "    test = TestBit()\n",
    "    test.test_insert_m_into_n()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success: test_insert_m_into_n\n"
     ]
    }
   ],
   "source": [
    "%run -i test_insert_m_into_n.py"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
